package com.spi.soconversion.orders;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;

import com.spi.soconversion.orders.conversion.InstallBaseWarrantyStatus;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCUtility;

public class SOCInstallBaseWarrantyList {

	private ArrayList<SOCInstallBaseWarranty> activeWarrantyList = new ArrayList<SOCInstallBaseWarranty>();
	private ArrayList<SOCInstallBaseWarranty> expiredWarrantyList = new ArrayList<SOCInstallBaseWarranty>();
	private ArrayList<SOCInstallBaseWarranty> signedWarrantyList = new ArrayList<SOCInstallBaseWarranty>();
	private Calendar currentCalendar = Calendar.getInstance();
	public boolean addInstallBaseWarranty(SOCInstallBaseWarranty sbw){
		boolean status = false;
//System.out.println(sbw.getWarrantyStatus());		
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.ACTIVE.toString()))
		{
			if(!activeWarrantyList.contains(sbw)) {
				status = activeWarrantyList.add(sbw);
			}
		}
		
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.SIGNED.toString()))
		{
			if(!signedWarrantyList.contains(sbw)) {
				status = signedWarrantyList.add(sbw);
			}
		}
		
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.EXPIRED.toString()))
		{
			if(!expiredWarrantyList.contains(sbw)) {
				status = expiredWarrantyList.add(sbw);
			}
		}
		
		return status;
	}
	
	public boolean removeInstallBaseWarranty(SOCInstallBaseWarranty sbw){
		boolean status = false;
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.ACTIVE.toString()))
		{
			status = activeWarrantyList.remove(sbw);
		}
		
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.SIGNED.toString()))
		{
			status = signedWarrantyList.remove(sbw);
		}
		
		if(sbw.getWarrantyStatus().equals(InstallBaseWarrantyStatus.EXPIRED.toString()))
		{
			status = expiredWarrantyList.remove(sbw);
		}
		
				
		return status;
	}

	public SOCInstallBaseWarranty getInstallBaseWarranty(String wtyType){
		
		SOCInstallBaseWarranty warranty=null;
		if(activeWarrantyList!=null && activeWarrantyList.size()>0)
		{
			warranty=activeWarrantyList.get(0);
		}
		else if(signedWarrantyList!=null && signedWarrantyList.size()>0)
		{
			warranty = signedWarrantyList.get(0);
		} 
		else if (expiredWarrantyList!=null && expiredWarrantyList.size()>0) 
		{
			warranty = expiredWarrantyList.get(0);
		}

		if(warranty==null && "EXT_WARRANTY".equals(wtyType))
		{
			if(expiredWarrantyList!=null && expiredWarrantyList.size()>0)
			{ 
				Collections.sort(expiredWarrantyList,
						new SOCInstallBaseWarrantyComparator());
				
				currentCalendar.setTime(new java.util.Date());
				java.sql.Timestamp wtyEndDate= expiredWarrantyList.get(0).getWarrantyEndDate();
				
				int monthDifference=0;
				if(wtyEndDate!=null)
				{
					Calendar wtyCal = Calendar.getInstance();
					wtyCal.setTimeInMillis(wtyEndDate.getTime());
					monthDifference= SOCUtility.getMonthDifference(wtyCal,currentCalendar);
				}
			
				if(monthDifference<=6)
				{
					warranty = expiredWarrantyList.get(0);
				}
			}				
		}
		
		return warranty;		
	}
	
	@Override
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		
		if(activeWarrantyList!=null && activeWarrantyList.size()>0)
		{
			Iterator<SOCInstallBaseWarranty> iter=activeWarrantyList.iterator();
    		while(iter.hasNext())
	    	{
    			sb.append(SOCConstants.NEW_LINE);
    			sb.append(iter.next().toString());  
			}    		    		
		}
		
		if(signedWarrantyList!=null && signedWarrantyList.size()>0)
		{
			Iterator<SOCInstallBaseWarranty> iter=signedWarrantyList.iterator();
    		while(iter.hasNext())
	    	{
    			sb.append(SOCConstants.NEW_LINE);
    			sb.append(iter.next().toString());  
			}    		    		
		}
		
		if(expiredWarrantyList!=null && expiredWarrantyList.size()>0)
		{
			Iterator<SOCInstallBaseWarranty> iter=expiredWarrantyList.iterator();
    		while(iter.hasNext())
	    	{
    			sb.append(SOCConstants.NEW_LINE);
    			sb.append(iter.next().toString());  
			}    		    		
		}
		return sb.toString();
	}
}
